home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************/
- /*
- /* serial.read.dll.c
- /* by Atul Butte
- /* Copyright ⌐ 1989 by Microsoft Corporation
- /* All Rights Reserved
- /*
- /* version 1.0
- /*
- /*
- /* This CALL/REGISTER will read from the serial port the number of chars
- /* specified.
- /*
- /* Excel usage:
- /*
- /* = Register( "serial library", "serial.read", "GHHJCG" )
- /* = Call( ref, portNumber, numberChars, maxTime, readConfigStr, )
- /*
- /* where
- /* portNumber = number of port (1 = modem, 2 = printer)
- /* numberChars = number of characters to read
- /* maxTime = maximum amount of time to wait for characters
- /* in 1/60 second units
- /* readConfigStr = configuration of communications protocol, etc
- /*
- /* The extra comma at the end of the CALL parameter list must be present.
- /*
- /***********************************************************************/
-
- /***********************************************************************/
- /*
- /* D E F I N E S
- /*
- /***********************************************************************/
-
- #define ROUTINE_NAME "serial.read"
- #define hNIL 0L
- #define pNIL 0L
-
- /***********************************************************************/
- /*
- /* I N C L U D E S
- /*
- /***********************************************************************/
-
- #include "serial.h"
- #include "error.h"
- #include "get_port.h"
- #include "get_read_flags.h"
-
- /***********************************************************************/
- /*
- /* P R O T O T Y P E S
- /*
- /***********************************************************************/
-
- /***********************************************************************/
- /*
- /* main
- /*
- /***********************************************************************/
-
- pascal char *main( port, cchDesired, timeDur, pszConfig, pstBuff )
-
- unsigned short port; /* serial port to use */
- unsigned short cchDesired; /* number of characters desired */
- unsigned long timeDur; /* maximum time to wait for chars (in ticks) */
- char *pszConfig; /* communications configuration string */
- char *pstBuff; /* buffer in which to return string */
-
- {
- register OSErr err; /* result code from Toolbox routines */
- ParamBlockRec param; /* parameter block for read/write */
-
- Boolean fEcho = false; /* flag for echoing characters */
- Boolean fEdit = false; /* flag for allowing edit characters */
- Boolean fStripLF = false; /* flag for stripping line feeds */
- Boolean fStrip8Bit = false; /* flag for stripping high bit */
- Boolean fAddLF = false; /* flag for adding LF after CR */
- Boolean fIgnore = false; /* flag for ignoring escape chars */
-
- register short cch = 0; /* number of characters received */
- long cchBuff = 0; /* number of characters waiting in read buffer */
- register unsigned long timeStop; /* time at which to stop */
- register char *pst; /* pointer to current character in buffer */
- short refIn; /* reference number for input port */
- short refOut; /* reference number for output port */
- register char ch; /* character read */
- char echoBackspace[3]; /* characters to send to echo Backspace */
- char echoLinefeed; /* characters to send to echo Linefeed */
-
- RememberA0();
- SetUpA4();
-
- if( cchDesired > 254 ) {
- display_error( "Number of characters to read cannot be greater than 254." );
- RestoreA4( );
- return( pstBuff );
- }
- if( pszConfig == pNIL ) {
- display_error( "The fourth parameter must be a configuration string." );
- RestoreA4( );
- return( pstBuff );
- }
- if( pstBuff == pNIL ) {
- display_error( "The fifth parameter must be a pointer to an empty string buffer." );
- RestoreA4( );
- return( pstBuff );
- }
- pst = pstBuff;
- *pst = 0;
- pst++;
-
- err = get_port( port, &refIn, &refOut );
- if( err != noErr ) {
- display_error( "Illegal port number." );
- RestoreA4( );
- return( pstBuff );
- }
-
- get_read_flags( pszConfig, &fEcho, &fEdit, &fStripLF, &fStrip8Bit, &fAddLF, &fIgnore );
-
- if( fEcho ) {
- echoBackspace[0] = kchBackspace;
- echoBackspace[1] = ' ';
- echoBackspace[2] = kchBackspace;
-
- if( fAddLF ) {
- echoLinefeed = kchLinefeed;
- }
- }
-
- timeStop = TickCount( ) + timeDur;
-
- while( cch < cchDesired ) {
- if( ( timeDur != 0 ) && ( TickCount( ) >= timeStop ) ) {
- break;
- }
- err = SerGetBuf( refIn, &cchBuff );
- if( err != noErr ) {
- display_error( "Error trying to count buffer." );
- break;
- }
- if( cchBuff == 0 )
- continue;
- param.ioParam.ioReqCount = 1;
- param.ioParam.ioBuffer = pst;
- param.ioParam.ioRefNum = refIn;
- err = PBRead( ¶m, false );
- if( err != noErr ) {
- display_error( "Error reading from serial port." );
- break;
- }
- if( fStrip8Bit ) {
- *pst &= 0x7f;
- }
- ch = *pst;
- if( ( (ch == kchBackspace) || (ch == kchDelete) ) && (fEdit) ) {
- cch -= 2;
- if( cch >= -1 ) {
- pst -= 2;
- } else {
- cch = -1;
- pst -= 1;
- }
- if( fEcho ) {
- param.ioParam.ioReqCount = 3;
- param.ioParam.ioRefNum = refOut;
- param.ioParam.ioBuffer = echoBackspace;
- err = PBWrite( ¶m, false );
- if( err != noErr ) {
- display_error( "Error echoing backspace to serial port." );
- break;
- }
- }
- } else if( fEcho ) { /* if not a backspace or delete */
- param.ioParam.ioReqCount = 1;
- param.ioParam.ioRefNum = refOut;
- err = PBWrite( ¶m, false );
- if( err != noErr ) {
- display_error( "Error echoing to serial port." );
- break;
- }
- if( (ch == kchReturn) && (fAddLF) ) {
- param.ioParam.ioReqCount = 1;
- param.ioParam.ioRefNum = refOut;
- param.ioParam.ioBuffer = &echoLinefeed;
- err = PBWrite( ¶m, false );
- if( err != noErr ) {
- display_error( "Error echoing linefeed to serial port." );
- break;
- }
- }
- }
- if( (ch == kchLinefeed) && (fStripLF) ) {
- pst--;
- cch--;
- }
- pst++;
- cch++;
- }
- *pstBuff = cch;
-
- RestoreA4( );
- return( pstBuff );
- }
-
- #include "get_read_flags.c"
- #include "get_port.c"
-
-